{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "\n",
    "from gs_quant.common import PayReceive\n",
    "from gs_quant.instrument import IRSwap, IRSwaption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.common import SwapSettlement"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None,\n",
    "              client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaptions = Portfolio()\n",
    "\n",
    "# you don't need to specify any parameters to get a valid trade.  All properties have defaults\n",
    "swaptions.append(IRSwaption())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get list of properties of an interest rate swaption\n",
    "# Many of these properties overlap with the IRSwap properties (outlined in example '010001_swap_trade_construction')\n",
    "IRSwaption.properties()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pay_or_receive can be a string of 'pay', 'receive', 'straddle' (an option strategy\n",
    "# where you enter into both a payer and receiver) or the PayReceive enum\n",
    "# relates to whether you expect to pay/receive fixed for the underlying. default is 'straddle'\n",
    "swaptions.append(IRSwaption(pay_or_receive=PayReceive.Receive))\n",
    "swaptions.append(IRSwaption(pay_or_receive='Receive'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# expiration_date is the date the option expires and may be a tenor relative\n",
    "# to the active PricingContext.pricing_date or a datetime.date, default is '10y'\n",
    "swaptions.append(IRSwaption(expiration_date='6m'))\n",
    "swaptions.append(IRSwaption(expiration_date=date(2022, 2, 12)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# strike is the rate at which the option can be exercised\n",
    "# It also represents the interest rate on the fixed leg of the swap if the swaption expires ITM. Defaults to Par Rate (ATM).\n",
    "# Can be expressed as 'ATM', 'ATM+25' for 25bp above par, a-100 for 100bp below par, 0.01 for 1%\n",
    "swaptions.append(IRSwaption(strike='ATM'))\n",
    "swaptions.append(IRSwaption(strike='ATM+50'))\n",
    "swaptions.append(IRSwaption(strike='a-100'))\n",
    "swaptions.append(IRSwaption(strike=.02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# effective_date is the start date of the underlying swap and may be a tenor relative\n",
    "# to the expiration_date or a datetime.date. Default is spot dates from expiration\n",
    "# For example, for a swaption w/ notional_currency as GBP, spot date is T+0, so effective_date = expiration_date.\n",
    "# for a swaption w/ notional_currency USD, spot is T+2 days and the effective_date is 2b after expiration_date\n",
    "swaptions.append(IRSwaption(effective_date='5b'))\n",
    "swaptions.append(IRSwaption(effective_date=date(2031, 2, 12)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# An IRSwaption's strike will resolve to an IRSwap's fixed_rate if the swaps' paramaters match and\n",
    "# the swaption's effective_date is equivalent to the swap's effective_date\n",
    "s = IRSwap(notional_currency='GBP', effective_date='10y')\n",
    "swp = IRSwaption(notional_currency='GBP', expiration_date = '10y', effective_date='0b')\n",
    "print(s.fixed_rate*100)\n",
    "print(swp.strike*100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# settlement is the settlement convention of the swaption and can be a string of:\n",
    "# 'Phys.CLEARED' (enter into cleared swap), 'Cash.PYU' (PYU - Par Yield Unadjusted,\n",
    "# cash payment calculated with PYU), 'Physical' (enter into a uncleared swap), \n",
    "# 'Cash.CollatCash' (collateralized, cash settled at expiry) or the SwapSettlement enum\n",
    "swaptions.append(IRSwaption(settlement=SwapSettlement.Phys_CLEARED))\n",
    "swaptions.append(IRSwaption(settlement='Cash.PYU'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# premium is the amount to be exchanged for the option contract. A positive premium will have a \n",
    "# negative impact on the PV. premium is a default is  0.\n",
    "swaptions.append(IRSwaption(premium=1e4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# premium_payment_date is the date when premium is exchanged.\n",
    "# premium_payment_date can be a datetime.date or a tenor. defaulted to spot dates from the\n",
    "# PricingContext.pricing_date\n",
    "swaptions.append(IRSwaption(premium_payment_date='5d'))\n",
    "swaptions.append(IRSwaption(premium_payment_date=date(2020, 2, 13)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# in some markets, the convention is for premium to be exchanged at expiration\n",
    "# this can be expressed by changing the premium_payment_date to the swaption's expiration_date\n",
    "swaptions.append(IRSwaption(premium_payment_date='5y', expiration_date='5y'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "swaptions.price()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}